package com.csf.project.controller;

import com.csf.project.entity.TlbbAreaDatas;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.csf.project.entity.User;
import com.csf.project.msg.ObjectRestResponse;
import com.csf.project.service.UserService;

import java.sql.SQLException;
import java.util.List;

/**
 * 用户: Controller类
 * 
 * @author generator
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户")
@Slf4j
public class UserController {
    
    
    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
//        //存K,V
//        redisTemplate.opsForSet().add("333",3333);
//        //存K,V
//        redisTemplate.opsForValue().set("eee","wwww");
//        //取
//        redisTemplate.opsForValue().get("eee");
//        //存K：[{k1,v1},{k2,v3}]
//        redisTemplate.opsForHash().put("hash","a","b");
//        //取输入K，取当前K下面的所有K,V
//        redisTemplate.opsForHash().get("hash","a");
//        //对K包所有V的设置过期时间
//        redisTemplate.expire("22",100, TimeUnit.DAYS);
//        //获取过期时间
//        redisTemplate.getExpire("22");
//        redisTemplate.opsForHash().put("hash","b","1");
//        //删除
//        redisTemplate.delete("hash");

    @ApiOperation(value ="使用Token获取用户信息",httpMethod = "POST")
    @PostMapping(value = "/info")
    public ObjectRestResponse<Object> getuserInfo(
            @ApiParam(name = "usertoken",value="获取到的Token" ,required =true)@RequestBody String usertoken
    ){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        User user = userServiceImpl.getUserInfo(usertoken);
        resultInfo.setData(user);
        return resultInfo;
    }

    /**
     * 检查用户是否存在
     * @param account
     * @return
     */
    @ApiOperation(value ="检查代理用户是否存在",httpMethod = "GET")
    @GetMapping("checkAccount")
    public  ObjectRestResponse<Object> checkAccount(
       @ApiParam(name = "account",value="用户账号" ,required =true)@RequestParam("account") String account
    ){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        Boolean regBool = userServiceImpl.checkAccount(account);
        resultInfo.setData(regBool);
        resultInfo.setRel(regBool);
        return resultInfo;
    }
    /**
     * 查询用户
     * @param user
     * @return
     */
    @ApiOperation(value ="查询代理用户是否存在",httpMethod = "GET")
    @GetMapping("queryData")
    @ResponseBody
    public  ObjectRestResponse<Object> queryData(User user){
        return userServiceImpl.queryData(user);
    }


    //用户注册接口
    @PostMapping("register")
    @ApiOperation(value ="代理用户注册接口",httpMethod = "POST")
    public ObjectRestResponse<Object> register(@RequestBody User user) {
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        Boolean regBool = userServiceImpl.register(user);
        resultInfo.setData(regBool);
        resultInfo.setRel(regBool);
        return resultInfo;

    }
    @ApiOperation(value ="修改代理用户信息",httpMethod = "POST")
    @PostMapping("updateUserInfo")
    public ObjectRestResponse<Object> updateUserInfo(@RequestBody User user){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        Boolean upBool = userServiceImpl.updateUserInfo(user);
        resultInfo.setData(upBool);
        resultInfo.setRel(upBool);
        return resultInfo;
    }

    @ApiOperation(value ="查询游戏用户剩余点数",httpMethod = "GET")
    @PostMapping("queryUserPoint")
    public ObjectRestResponse<Object> queryUserPoint(
            @ApiParam(name = "num",value="区服" ,required =true)@RequestParam("num") Integer num,
            @ApiParam(name = "account",value="账号" ,required =true)@RequestParam("account") String account){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        String s = userServiceImpl.queryUserPoint(num, account);
        resultInfo.setData(s);
        resultInfo.setRel(true);
        return resultInfo;
    }

    @ApiOperation(value ="更新游戏用户点数",httpMethod = "POST")
    @PostMapping("updataPoint")
    public ObjectRestResponse<Object> updateUserInfo(
            @ApiParam(name = "num",value="区服" ,required =true)@RequestParam("num") Integer num,
            @ApiParam(name = "account",value="账号" ,required =true)@RequestParam("account") String account,
            @ApiParam(name = "point",value="点数" ,required =true)@Param("point") Integer point){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        String s = userServiceImpl.updataPoint(num, account, point);
        resultInfo.setData(s);
        resultInfo.setRel(true);
        return resultInfo;
    }

    @ApiOperation(value ="生成随机验证码发送到邮箱",httpMethod = "GET")
    @GetMapping("getRandCode")
    public ObjectRestResponse<Object> getRandCode(
            @ApiParam(name = "mail",value="邮箱" ,required =true)@RequestParam("mail") String mail
    ){
        ObjectRestResponse<Object> resultInfo = new ObjectRestResponse<Object>();
        Boolean s = userServiceImpl.getRandCode(mail);
        resultInfo.setData(s);
        resultInfo.setRel(s);
        return resultInfo;
    }

    @GetMapping("/test")
    @ApiOperation(value ="测试接口不对外开放",httpMethod = "GET")
    public  Object test(){
//        Boolean randCode = userServiceImpl.getRandCode("86061058@qq.com");
//        redisTemplate.opsForValue().set("bb","222cjenmg");
        //String str =tlbbAreaDatas.getOneurl()+"=="+tlbbAreaDatas.getOneusername()+"==="+tlbbAreaDatas.getOnepasswd();
        //System.out.println(str);
//        log.info(redisTemplate.opsForValue().get("aa").toString());
        return "测试接口不对外开放";
    }
}